var subjectId;
if(T.p("subjectId")!="undefined" &&T.p("subjectId")!=null ){
    subjectId = T.p("subjectId");
}
var subjectList = T.getDictDynas("subject");
var statusList = T.getDictItems("sys_status");
var layerFlag=T.p("layerFlag");
var jobId = T.p("jobId");
if(layerFlag=="undefined"||layerFlag==null){
    layerFlag = false;
}else{
    layerFlag =true;
}
var isShowCond = T.p("isShowCond");
if(isShowCond=="undefined"||isShowCond==null){
    isShowCond = true;
}else{
    isShowCond =true;
}

var returnParams = T.p("returnParams");
var jqDataType = "json";
if(null != returnParams){
    jqDataType = "local";
}

$(function () {
    $("#jqGrid").jqGrid({
        url: '../work/list',
        datatype:jqDataType,
        postData:{jobId:jobId,subjectId:subjectId},
        colModel: [
			{ label: 'id', name: 'id', width: 50, key: true },
			{ label: '名称', name: 'workName', width: 80 },
			{ label: '专题', name: 'subjectId', width: 50 , formatter: function (value, options, row) {
                 return T.showDictDynaName("subject",value);
            }},
			{ label: '依赖类型', name: 'referType', width: 50 , formatter: function(value, options, row){
                switch(value)
                {
                    case "referSql":
                        return '依赖sql';
                        break;
                    case "referTaskId":
                        return '依赖任务ID';
                        break;
                    default:
                        return '<font color=#aaa>未设置</font>';
                }
            }},
			{ label: '失败策略', name: 'misfire', width: 80 , formatter: function(value, options, row){
                switch(value)
                {
                    case "misfireWarn":
                        return '失败预警';
                        break;
                    case "misfireTry":
                        return '失败重试';
                        break;
                    default:
                        return '<font color=#aaa>未设置</font>';
                }
            }},
			{ label: '状态', name: 'status', width: 80 , formatter: function (value, options, row) {
                    return T.showDictItemName('sys_status',value);
			}},
			{ label: '描述', name: 'remark', width: 80 },
            { label:'操作', name:'actions', sortable:false,width:80,hidden:layerFlag,formatter:function (value, options, row, act) {
                 var opts =  [];
                    opts.push('<a href="javascript:" class="btnList" title="编辑作业项" onclick="vm.updateRow('+row.id+')" ><i class="fa fa-pencil"></i></a>&nbsp;');
                    opts.push('<a href="javascript:" class="btnList" title="作业项明细" onclick="vm.dtlMgr('+row.id+')"><i class="fa fa-navicon"></i></a>&nbsp;');
                    if (row.status == 1){
                        opts.push('<a href="javascript:" class="btnList" title="停用作业项" data-confirm="确认要停用该作业项吗？" data-click="vm.changeStatus('+row.id+',0)"><i class="glyphicon glyphicon-ban-circle"></i></a>&nbsp;');
                    }
                    if (row.status == 0){
                        opts.push('<a href="javascript:" class="btnList" title="启用作业项" data-confirm="确认要启用该作业项吗？" data-click="vm.changeStatus('+row.id+',1)"><i class="glyphicon glyphicon-ok-circle"></i></a>&nbsp;');
                    }

                    opts.push('<a href="javascript:" class="btnMore" title="更多操作"><i class="fa fa-chevron-circle-right"></i></a>&nbsp;');
                    opts.push('<div class="moreItems">');
                    opts.push('<a href="javascript:" class="btn btn-default btn-xs " title="管理作业项私有参数" onclick="vm.paramMgr('+row.id+')"><i class="fa fa-file-code-o"></i> 参数管理</a>&nbsp;');
                    opts.push('<a href="javascript:" class="btn btn-default btn-xs " title="回溯历史版本"><i class="fa fa-file-text"></i> 版本回溯</a>&nbsp;');
                    opts.push('</div>');
                    return opts.join('');
                }
            }
        ],
		viewrecords: true,
        height: 800,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
			root: "page.data",
			page: "page.pageNo",
			total: "page.pageCount",
			records: "page.count"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }, ondblClickRow: function(id){
            vm.itemMgr(id);
        },loadComplete:function () {
            btnMoreEventBind();
        }
    });
});


var vm = new Vue({
	el:'#rrapp',
	data:{
        isShowCond:isShowCond,
        btnText:'隐藏',
        qryForm:{
            subjectId:subjectId,
            workName:"",
            status:""
        },
        work:{},
        subjectList:subjectList
	},
	methods: {
        setSubjectId : function (pSubjectId) {
            subjectId =pSubjectId;
            this.qryForm.subjectId=subjectId;
        },
	    reset: function(){
            this.qryForm.subjectId=subjectId;
            this.qryForm.workName="";
            this.qryForm.status="";
        },
        skdLog: function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }

            location.href = "/skd/scheduleLog.html?workId=" +id+ "&subjectId="+vm.qryForm.subjectId+"&isShowCond="+vm.isShowCond;

        },
        query: function () {
            $("#jqGrid").jqGrid('setGridParam',{
                datatype:'json',
                postData:{'subjectId': vm.qryForm.subjectId,
                    'workNameLike': '%'+vm.qryForm.workName+'%',
                    'status':vm.qryForm.status
                },
                page:1
            }).trigger("reloadGrid");
        },
        toggle:function(){
            this.isShowCond = !this.isShowCond;

            if(this.isShowCond==true){
                this.btnText = "隐藏"
            }else if(this.isShowCond==false){
                this.btnText = "查询"
            }

        },
        create:function(){
            location.href = "work_add.html?subjectId="+this.qryForm.subjectId+"&isShowCond="+this.isShowCond;
        },
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			location.href = "work_add.html?id="+id +"&subjectId="+this.qryForm.subjectId+"&isShowCond="+this.isShowCond;
		},
        updateRow: function (id) {
            if(id == null){
                return ;
            }
            location.href = "work_add.html?id="+id;
        },
        changeStatus: function (id,status) {
            var url =  "../work/updateKeySelective";
            vm.work.id = id;
            vm.work.status = status;
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.work),
                success: function(r){
                    if(r.code === 0){
                        $("#jqGrid").trigger("reloadGrid");
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../work/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        itemMgr: function (id) {
            if(id == null){
                return ;
            }
            location.href = "/work/workItem.html?workId=" + id+ "&subjectId="+vm.qryForm.subjectId+"&isShowCond="+vm.isShowCond+"&returnParams="+encodeURI(JSON.stringify(vm.qryForm))
        },
        paramMgr:function (id) {
            if(id == null){
                return ;
            }
            location.href = "/work/workParams.html?belongType=2&belongId="+id;
        },
        test:function(tt){
	        console.log(tt);
        },
        runOnce: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要立即执行选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "../work/run",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        }
	}
});



if(returnParams != null && returnParams != 'null'){
    var param = eval('(' + decodeURI(returnParams) + ')');
    vm.qryForm = param;
    setTimeout(function(){
    $("#jqGrid").jqGrid('setGridParam',{
        datatype:'json',
        postData:{'subjectId': param.subjectId,
            'workNameLike': '%'+param.workName+'%',
            'status':param.status
        },
        page:1
    }).trigger("reloadGrid")},100);

}